package GuiController;


import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

/**
 * Opens a dialog window and select a system file path
 * @author Christian Braun
 * @version v2.0 final
 */


public class FileLoader {


	private JFileChooser fj = new JFileChooser();
	private File file ;
	
        /**
         * FileLoder constructor
         */
	public FileLoader (){	
	}
	
        /**
         * Opens a FileLoader dialog
         * @param lOrS if lOrS = "load" a file load dialog opens
         *             if lOrS = "save" a file save dialog opens
         */
        public void openDialog (String lOrS){
            if (lOrS.equals("load")){
            fj.setDialogTitle("Bitte wählen Sie eine BVH-Datei aus");
		 fj.setFileFilter(new FileFilter() {
                    public boolean accept(File f) {
                    return f.getName().toLowerCase().endsWith(".bvh") || f.isDirectory();
                    }
                           
                    public String getDescription() {
                    return "BVH - Dateien  (*.bvh)";
                    }
                });
            
            if (fj.showOpenDialog(null)==fj.APPROVE_OPTION)	   
	    this.file = fj.getSelectedFile();
            fj.setCurrentDirectory(file);
            }
            else{
                fj.setDialogTitle("Bitte wählen Sie ein Verzeichnis aus");
                 fj.setFileFilter(new FileFilter() {
                    public boolean accept(File f) {
                    return f.getName().toLowerCase().endsWith(".bvh") || f.isDirectory();
                    }
                    
                    public String getDescription() {
                    return "BVH - Dateien  (*.bvh)";
                    }
                });
                fj.showSaveDialog(fj);
		this.file = fj.getSelectedFile();
                fj.setCurrentDirectory(file);
            }
            
        }
        
        
        /**
         * 
         * @return the choosen File
         */
	public File getFile(){
		return this.file;
	}
        
      
        /**
         * 
         * @return the name of the file as string
         */
    @Override
        public String toString(){
            return file.getName();
        }
}

